<?php

namespace app\admin\controller;

use think\Controller;

class Index extends Controller
{
    //重复登录过滤
    public function initialize()
    {
        if (session('?admin.id')){
            $this -> redirect('admin/home/index');
        }
    }

    //后台登录
    public function login()
    {
        if (request()->isAjax()){
            $data = [
                'username' => input('post.username'),
                'password' => input('post.password')
            ];
            $result = model('common/Admin')->login($data);
            if ($result == 1){
                $this -> success('登录成功！','admin/home/index');
            } else {
                $this -> error($result);
            }
        }
        return view();
    }

    //注册
    public function register()
    {
        if (request()->isAjax()){
            $data = [
                'username' => input('post.username'),
                'password' => input('post.password'),
                'conpass' => input('post.conpass'),
                'nickname' => input('post.nickname'),
                'email' => input('post.email'),
            ];
            $result = model('common/Admin') -> register($data);
             if ($result == 1){
                $this -> success('注册成功！','admin/index/login');
             } else {
                $this -> error($result);
             }
        }
        return view();
    }

    //忘记密码，发送验证码
    public function forget()
    {
        if (request()->isAjax()){
             $data = [
                 'username' => input('post.username')
             ];
             $result = model('common/Admin') -> forget($data);
             if ($result == 1){
                 $this -> success('验证码发送成功！'.session('code')['num'].'---'.session('code')['settime']);
             } else {
                 $this -> error($result);
             }
        }
        return view();
    }

    //忘记密码，修改密码
    public function reset()
    {
        if (request()->isAjax()){
            $data = [
                'code' => input('post.code'),
                'password' => input('post.password'),
                'conpass' => input('post.conpass')
            ];
            $result = model('common/Admin') -> reset($data);
            if ($result == 1) {
                $this -> success('密码已重置成功！','admin/index/login');
            } else {
                $this -> error($result);
            }
        }
    }





}
